package com.module.rotary.dialog;

import android.animation.ValueAnimator;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.dq.ad.AdManager;
import com.dq.ad.UMManager;
import com.dq.util.ILog;
import com.dq.util.NetWorkUtil;
import com.dq.util.ToastUtil;
import com.module.rotary.R;
import com.module.rotary.activity.RotaryActivity;
import com.module.rotary.config.RotaryConfig;
import com.module.rotary.entity.RotaryEntity;
import com.module.rotary.listener.RotateListener;
import com.module.rotary.view.WheelSurfView;

import org.litepal.LitePal;

import java.util.List;
import java.util.Random;

public class DialogRotary extends AlertDialog {

    private Activity activity;
    private ImageView imgClose;
    private Button btnVideo;
    private WheelSurfView wheelSurfView;
    private OnDialogClickLister clickLister;

    public DialogRotary(@NonNull Activity a) {
        super(a, R.style.dialog_loading);
        this.activity = a;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_rotary);
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        initView();
    }

    private void initView() {
        UMManager.onEvent(activity,"Ad1Show");
        imgClose = findViewById(R.id.imgClose);
        btnVideo = findViewById(R.id.btnVideo);
        wheelSurfView = findViewById(R.id.wheelView);

        final String[] des = new String[]{"华为mate40手机", "手机壳", "话费充值券", "华为蓝牙手机", "蓝牙音箱", "再玩一次"};

        wheelSurfView.setStartEnable(false);
        wheelSurfView.setRotateListener(new RotateListener() {
            @Override
            public void rotateEnd(int position, String desValue) {
                if (position == 1) {
                    desValue = "再玩一次";
                } else if (position == 2) {
                    desValue = "蓝牙音箱";
                } else if (position == 3) {
                    desValue = "华为蓝牙手机";
                } else if (position == 4) {
                    desValue = "话费充值券";
                } else if (position == 5) {
                    desValue = "手机壳";
                } else if (position == 6) {
                    desValue = "华为mate40手机";
                }
                ILog.e("抽奖结果:" + position + "," + desValue);
                position = RotaryActivity.getIndex(des, desValue) + 1;//数据库中对应商品的商品id
                if (position == 6) {//再来一次
                    RotaryConfig.setLotteryNumber(RotaryConfig.getLotteryNumber() + 1);
                    return;
                }

                List<RotaryEntity> entityList = LitePal.where("id = ?", String.valueOf(position)).find(RotaryEntity.class);
                if (entityList.size() > 0) {
                    RotaryEntity rotaryEntity = entityList.get(0);//抽中了某个商品（如mate40手机）

                    int debrisNumber = 0;//当前用户抽到的碎片数量
                    if (rotaryEntity.isSmoke1) {
                        debrisNumber++;
                    }
                    if (rotaryEntity.isSmoke2) {
                        debrisNumber++;
                    }
                    if (rotaryEntity.isSmoke3) {
                        debrisNumber++;
                    }
                    if (rotaryEntity.isSmoke4) {
                        debrisNumber++;
                    }
                    if (rotaryEntity.isSmoke5) {
                        debrisNumber++;
                    }

                    if (debrisNumber >= 4) {
                        //如果用户抽到4张碎片以后，禁止再继续抽取
                        return;
                    }

                    int random = new Random().nextInt(5) + 1;

                    switch (random) {
                        case 1:
                            rotaryEntity.setSmoke1(true);
                            break;
                        case 2:
                            rotaryEntity.setSmoke2(true);
                            break;
                        case 3:
                            rotaryEntity.setSmoke3(true);
                            break;
                        case 4:
                            rotaryEntity.setSmoke4(true);
                            break;
                        case 5:
                            rotaryEntity.setSmoke5(true);
                            break;
                    }
                    rotaryEntity.save();
                } else {
                    ToastUtil.showShortToast(activity, "此次抽奖失败");
                }

                dismiss();
                new DialogResult(activity)
                        .setContent(desValue)
                        .setPos(position)
                        .setBtnContent("继续抽奖")
                        .then(new DialogResult.OnDialogClickLister() {
                            @Override
                            public void onClose(Dialog dialog) {
                                dialog.dismiss();
                            }

                            @Override
                            public void onSeeVideo(Dialog dialog) {
                                dialog.dismiss();
                                AdManager.getInstance().showRewardVideoAD(activity);
                            }

                            @Override
                            public void onReceive(Dialog dialog) {
                                dialog.dismiss();
                            }
                        }).show();
            }

            @Override
            public void rotating(ValueAnimator valueAnimator) {

            }

            @Override
            public void rotateBefore(ImageView goImg) {

            }
        });

        initLister();
    }

    private void rotateBefore() {
        if (!NetWorkUtil.isNetworkConnected(activity)) {
            ToastUtil.showShortToast(activity, "当前网络状态不佳，请检查网络后再试");
            return;
        }

        /** 抽奖次数不够不再进行抽奖 **/
        if (RotaryConfig.getLotteryNumber() < 1) {
            dismiss();
            activity.startActivity(new Intent(activity, RotaryActivity.class));
            return;
        }

        /** 模拟位置 **/
        int position = new Random().nextInt(11);
        ILog.e("随机数:" + position);
        if (position > 6 || position == 2 || position == 0) {
            position = 2;
        }
        ILog.e("最终结果:" + position);
        wheelSurfView.startRotate(position);

        /** 抽奖次数-1 **/
        RotaryConfig.setLotteryNumber(RotaryConfig.getLotteryNumber() - 1);
    }

    private void initLister() {
        imgClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UMManager.onEvent(activity,"Ad1Close");
                if (clickLister != null) {
                    clickLister.onClose(DialogRotary.this);
                }
            }
        });
        btnVideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UMManager.onEvent(activity,"Ad1Pay");
                AdManager.getInstance().showRewardVideoAD(activity, new AdManager.OnRewardVideoADListener() {
                    @Override
                    public void onClosed() {
                        rotateBefore();
                    }

                    @Override
                    public void onReward() {}

                    @Override
                    public void onFailed(int i, Object adError) {
                        rotateBefore();
                    }
                });
            }
        });
    }



    public DialogRotary then(OnDialogClickLister lister) {
        this.clickLister = lister;
        return this;
    }

    public interface OnDialogClickLister {
        void onClose(Dialog dialog);//关闭弹窗
    }

}